package splug.cjk;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class BBSCarPoolSearchResult1 extends ListActivity implements OnItemClickListener{

	private String id;
	private ArrayList<Content> content;
	private ArrayList<BBSCarPoolContent> sub_content;
	private ContentAdapter m_adapter;
	private Button back;

	private class ContentAdapter extends ArrayAdapter<Content> {

		private ArrayList<Content> items;

		public ContentAdapter(Context context, int textViewResourceId, ArrayList<Content> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.list_property, null);
			}
			Content p = items.get(position);
			if (p != null) {
				TextView tt = (TextView) v.findViewById(R.id.toptext);
				TextView bt = (TextView) v.findViewById(R.id.bottomtext);
				if (tt != null){
					tt.setText(p.getTitle());
				}
				if(bt != null){
					bt.setText("글쓴이: "+ p.getId());
				}
			}
			return v;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bbs_carpool_search_result);
		setTitle("검색 결과");

		Bundle b = this.getIntent().getExtras();
		id = String.valueOf(b.getString("ID"));
		content = (ArrayList<Content>) b.getSerializable("TITLE");
		sub_content = (ArrayList<BBSCarPoolContent>) b.getSerializable("SUB");
		
		back = (Button) findViewById(R.id.btn_carpool_search_result_back);
		
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		m_adapter = new ContentAdapter(BBSCarPoolSearchResult1.this, R.layout.list_property, content); // 어댑터를 생성합니다.
		setListAdapter(m_adapter);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub

	}

	//리스트가 클릭되었을 때의 이벤트
	@Override
	protected void onListItemClick(ListView l, View v, int position, long aaa) {
		super.onListItemClick(l, v, position, aaa);

		Intent intent = new Intent(BBSCarPoolSearchResult1.this, BBSCarPoolTab1Content.class);
		Bundle b = new Bundle();
		b.putString("ID", id);
		b.putString("AUTHOR", content.get(position).getId());
		b.putInt("NUMBER", sub_content.get(position).getNumber());
		b.putString("CONTENT", sub_content.get(position).getContent());
		b.putString("TIME", sub_content.get(position).getTime());
		b.putString("START", sub_content.get(position).getStart());
		b.putString("FINISH", sub_content.get(position).getFinish());
		b.putString("DATE", sub_content.get(position).getDate());
		intent.putExtras(b);
		startActivity(intent);
	}
}
